﻿using System;
using System.Collections.Generic;
using shfa.snomtastic.admin.components;
using shfa.snomtastic.data;
using shfa.snomtastic.data.business;
using shfa.snomtastic.data.security;

namespace shfa.snomtastic.admin.editcontrols
{
    public partial class eAppConfig : EditBase
    {

        #region Public Members



        #endregion

        #region Page

        protected void Page_Load(object sender, EventArgs e)
        {
            bindControls();
        }


        #endregion

        #region Helpers

        private void bindControls()
        {
            AppConfig ac = new AppConfig();
            txtAdRoot.Text = ac.GetItemValue("adRoot");
            txtAdFilter.Text = ac.GetItemValue("adFilter");
            txtAdLogin.Text = ac.GetItemValue("adLogin");
            txtAdPassword.Text = ac.GetItemValue("adPassword");
            chkEnableErrorMode.Checked = (ac.GetItemValue("enableErrorMode").ToLower() == "true");
            txtFailModeDelay.Text = ac.GetItemValue("failModeDelay");
            txtMaxRegAttempts.Text = ac.GetItemValue("MaxRegAttempts");
            txtHttp_Pass.Text = ac.GetItemValue("http_pass");
            txtHttp_User.Text = ac.GetItemValue("http_user");
            txtSiteRoot.Text = ac.GetItemValue("siteRoot");
            txtLogHours.Text = ac.GetItemValue("logHours");
            txtDomain.Text = ac.GetItemValue("Domain");
            txtOcsServer.Text = ac.GetItemValue("OcsServer");
            txtTransport.Text = ac.GetItemValue("Transport");
        }

        

        private void SaveSettings()
        {
            AppConfig ac = new AppConfig();
            RijndaelEnhanced re = new RijndaelEnhanced();

            AppConfigController acc = new AppConfigController();
            List<AppConfigInfo> aciList = acc.SelectAppConfigs();

            foreach (AppConfigInfo aci in aciList)
            {
                switch (aci.Name)
                {
                    case "adRoot":
                        aci.Value = txtAdRoot.Text;
                        break;
                    case "adFilter":
                        aci.Value = txtAdFilter.Text;
                        break;
                    case "adLogin":
                        aci.Value = txtAdLogin.Text;
                        break;
                    case "adPassword":
                        if (txtAdPassword.Text != string.Empty)
                        {
                            aci.Value = re.Encrypt(txtAdPassword.Text);
                        }
                        break;
                    case "siteRoot":
                        if (txtSiteRoot.Text != string.Empty)
                        {
                            if (txtSiteRoot.Text[txtSiteRoot.Text.Length - 1] != '/')
                            {
                                txtSiteRoot.Text += "/";
                            }
                        }
                        aci.Value = txtSiteRoot.Text;
                        break;
                    case "http_user":
                        aci.Value = txtHttp_User.Text;
                        break;
                    case "http_pass":
                        if (txtHttp_Pass.Text != string.Empty)
                        {
                            aci.Value = re.Encrypt(txtHttp_Pass.Text);
                        }
                        break;
                    case "enableErrorMode":
                        aci.Value = chkEnableErrorMode.Checked.ToString();
                        break;
                    case "failModeDelay":
                        aci.Value = txtFailModeDelay.Text;
                        break;
                    case "MaxRegAttempts":
                        aci.Value = txtMaxRegAttempts.Text;
                        break;
                    case "logHours":
                        aci.Value = txtLogHours.Text;
                        break;
                    case "Domain":
                        aci.Value = txtDomain.Text;
                        break;
                    case "OcsServer":
                        aci.Value = txtOcsServer.Text;
                        break;
                    case "Transport":
                        aci.Value = txtTransport.Text;
                        break;
                    default:
                        break;
                }

                acc.UpdateAppConfig(aci);
            }

            bindControls();



        }

        #endregion

        #region EditBase

        public override void EditBase_ActionEvent(object sender, ActionClickArgs e)
        {
            switch (e.ActionName)
            {
                case "Save":
                    SaveSettings();
                    break;
                default:
                    break;
            }
        }

        public override List<ActionInfo> ActionList
        {
            get
            {
                List<ActionInfo> al = new List<ActionInfo>();
                al.Add(new ActionInfo("Save", "Save Settings", "~/images/icons/save.png"));

                return al;
            }
        }

        #endregion


    }
}